<template>
	<view class="container">
		<!-- 父级 scroll-view，确保两列同步滑动 -->
		<scroll-view class="content" scroll-y="true" refresher-enabled="true" :refresher-triggered="isRefreshing"
			@refresherrefresh="onRefresh">
			<view class="project-list">
				<!-- 每行显示两个项目 -->
				<view class="project-item" v-for="(item, index) in projectList" :key="index">
					<view class="card" @click="goToDetail(item.id)">
						<view class="card-header">
							<view class="card-title">项目: {{ item.name }}</view>
							<view class="card-project">项目ID: {{ item.id }}</view>
						</view>
						<view class="card-info">
							<view class="info-row">
								<view class="label">状态:</view>
								<dict-type typeName="project_status" :value="item.status"></dict-type>
							</view>
							<view class="info-row">
								<view class="label">开始日期:</view>
								<view class="value">{{ formatDate(item.startDate) }}</view>
							</view>
							<view class="info-row">
								<view class="label">结束日期:</view>
								<view class="value">{{ formatDate(item.endDate) }}</view>
							</view>
							<view class="info-row">
								<view class="label">创建人ID:</view>
								<view class="value">{{ item.createdBy }}</view>
							</view>
							<view class="info-row">
								<view class="label">创建人姓名:</view>
								<view class="value">{{ item.createdName }}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		listProject
	} from "@/api/project/project";
	import DictType from "../../components/dictType/dictType.vue";

	export default {
		name: "Project",
		components: {
			DictType
		},
		data() {
			return {
				loading: true,
				projectList: [],
				isRefreshing: false, // 控制刷新状态
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: null,
					status: null,
					startDate: null,
					endDate: null,
					createdBy: null,
					createdName: null
				},
			};
		},
		created() {
			this.getList();
		},
		methods: {
			async getList() {
				this.loading = true;
				const taskRes = await listProject(this.queryParams)
				this.projectList = taskRes.rows;
				this.loading = false;
			},
			async onRefresh() {
				this.isRefreshing = true; // 停止刷新
				this.getList(); // 获取最新数据
				console.log(this.isRefreshing)
			},
			formatDate(timestamp) {
				if (!timestamp) return '';
				const date = new Date(timestamp);
				return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
			},
			goToDetail(id) {
				uni.navigateTo({
					url: `/pages/project/projectInit/index?projectId=${id}`
				});
			}
		}
	};
</script>

<style scoped>
	.container {
		display: flex;
		padding: 20rpx;
		background-color: #f8f8f8;
		min-height: 100vh;
	}

	.content {
		display: flex;
		flex-direction: column;
		width: 100%;
	}

	.project-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 10rpx;
	}

	.project-item {
		width: 48%;
		/* 每行显示两个项目 */
		margin-bottom: 10rpx;
	}

	.card {
		background: #fff;
		border-radius: 12rpx;
		padding: 16rpx;
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.card-header {
		display: flex;
		justify-content: space-between;
		margin-bottom: 12rpx;
	}

	.card-title {
		font-size: 30rpx;
		font-weight: bold;
		color: #222;
	}

	.card-project {
		font-size: 26rpx;
		color: #888;
	}

	.card-info {
		display: flex;
		flex-direction: column;
	}

	.info-row {
		display: flex;
		justify-content: space-between;
		margin-bottom: 8rpx;
	}

	.label {
		font-size: 28rpx;
		color: #666;
	}

	.value {
		font-size: 28rpx;
		color: #333;
	}

	.status {
		color: #ff9800;
		font-weight: bold;
	}
</style>